package model;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import net.ck.hzq.R;

import java.util.List;

import Bean.AchievementBean;
import coom.Constants;
import ui.activity.profit.ProfitDetailsAct;

public class AchievementListviewAdapter extends BaseAdapter {

    private LayoutInflater mInflater;
    private List<AchievementBean.DataBean.ItemsBean> mDatas;

    Context mContext;

    public AchievementListviewAdapter(Context context, List<AchievementBean.DataBean.ItemsBean> datas) {
        mInflater = LayoutInflater.from(context);
        mDatas = datas;
        mContext = context;
    }

    public void setData(List<AchievementBean.DataBean.ItemsBean> data) {
        mDatas.clear();
        if (data != null) {
            mDatas.addAll(data);
        }
        notifyDataSetChanged();
    }

    public List<AchievementBean.DataBean.ItemsBean> getData() {
        return mDatas;
    }

    @Override
    public int getCount() {
        return mDatas.size();
    }

    @Override
    public Object getItem(int position) {
        return mDatas.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.item_listview, parent, false); //加载布局
            holder = new ViewHolder();

            holder.item_month = convertView.findViewById(R.id.item_month);
            holder.item_newbusinesses = convertView.findViewById(R.id.item_newbusinesses);
            holder.item_newallies = convertView.findViewById(R.id.item_newallies);
            holder.item_totaltransaction = convertView.findViewById(R.id.item_totaltransaction);
            holder.item_myincome = convertView.findViewById(R.id.item_myincome);
            holder.item_txt1 = convertView.findViewById(R.id.item_txt1);
            holder.item_txt2 = convertView.findViewById(R.id.item_txt2);
            holder.item_txt3 = convertView.findViewById(R.id.item_txt3);
            holder.item_txt4 = convertView.findViewById(R.id.item_txt4);
            holder.item_txt5 = convertView.findViewById(R.id.item_txt5);
            holder.item_txt6 = convertView.findViewById(R.id.item_txt6);
            holder.item_image1 = convertView.findViewById(R.id.item_image1);
            holder.item_image2 = convertView.findViewById(R.id.item_image2);
            holder.item_linear1 = convertView.findViewById(R.id.item_linear1);
            holder.item_linear2 = convertView.findViewById(R.id.item_linear2);
            holder.performDetails = convertView.findViewById(R.id.perform_details);
            convertView.setTag(holder);

        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        try {

        } catch (Exception e) {

        }
        String[] aa = mDatas.get(position).getTime().split("-");
        String mouth = "0";
        if (aa.length == 2) {
            mouth = aa[1];
        }

        holder.item_month.setText(mDatas.get(position).getTime() + "月业绩");
        holder.item_newbusinesses.setText(mDatas.get(position).getMerchant() + "");
        holder.item_newallies.setText(mDatas.get(position).getAlly() + "");
        holder.item_totaltransaction.setText(mDatas.get(position).getAllmoney() + "");
        holder.item_myincome.setText(mDatas.get(position).getMymoney() + "");
        holder.item_txt1.setText("MPOS: " + mDatas.get(position).getZyMpos());
        holder.item_txt2.setText("传统POS: " + mDatas.get(position).getZyPos());
        holder.item_txt3.setText("MPOS: " + mDatas.get(position).getAllyMpos());
        holder.item_txt4.setText("传统POS: " + mDatas.get(position).getAllyPos());
        holder.item_txt5.setText("直营交易: " + mDatas.get(position).getZymoney());
        holder.item_txt6.setText("合伙人交易: " + mDatas.get(position).getAllymoney());

        final LinearLayout linear1 = holder.item_linear1;
        final LinearLayout linear2 = holder.item_linear2;
        final ImageView iv = holder.item_image1;
        final ImageView iv2 = holder.item_image2;

        holder.item_image1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (linear1.getVisibility() == View.GONE) {
                    linear1.setVisibility(View.VISIBLE);
                    iv.setImageResource(R.drawable.arrow_blue);
                } else {
                    linear1.setVisibility(View.GONE);
                    iv.setImageResource(R.drawable.arrow);
                }
            }
        });

        holder.item_image2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (linear2.getVisibility() == View.GONE) {
                    linear2.setVisibility(View.VISIBLE);
                    iv2.setImageResource(R.drawable.arrow_blue);
                } else {
                    linear2.setVisibility(View.GONE);
                    iv2.setImageResource(R.drawable.arrow);
                }
            }
        });
        holder.performDetails.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(mContext, ProfitDetailsAct.class);
                intent.putExtra(Constants.KEYSTRING, mDatas.get(position).getTime());
                mContext.startActivity(intent);
            }
        });

        return convertView;
    }

    private class ViewHolder {
        TextView item_month;
        TextView item_newbusinesses;
        TextView item_newallies;
        TextView item_totaltransaction;
        TextView item_myincome;
        ImageView item_image1;
        ImageView item_image2;
        LinearLayout item_linear1;
        LinearLayout item_linear2;
        TextView item_txt1, item_txt2, item_txt3, item_txt4, item_txt5, item_txt6;
        TextView performDetails;
    }

}
